<?php

class App_Model_DbTable_Article_Search_Threads extends Ext_Db_Table  
{  
   /** Table name */  
    protected $_name = 'article_search_threads';
    
    private $_articleRow = array();
    
    public function getRowByArticleId($articleId)
    {        
        if(!$articleId)
        {
            return "";
        }
        
        if(isset($this->_articleRow[$articleId]))
        {
            return $this->_articleRow[$articleId];
        }
        
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" articleId = ? ", $articleId);
        $row = $this->fetchRow($safe);
        
        if (!$row) {
            return "";
        }
        $_articleRow = $row->toArray();
        
        $this->_articleRow[$articleId] = $_articleRow;
        
        return $_articleRow;
    }
    
    public function del($articleId)
    {
        if(!$articleId)
        {
            return "";
        }
        
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" articleId = ? ", $articleId);
        $rows_affected = $this->delete($safe);
        if($rows_affected){
            $this->_unsetArticleRow($articleId);
        }
        return "";
    }
    
    public function addNew($inputArray)
    {
        $sql_data_array = array();
        if(array_key_exists('articleId', $inputArray)) $sql_data_array['articleId'] = $inputArray['articleId'];
        if(array_key_exists('userId', $inputArray)) $sql_data_array['userId'] = $inputArray['userId'];
        if(!$sql_data_array){
            return "";
        }
        
        try {
            $rows_affected = $this->insert($sql_data_array);
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
        
    }
    
    public function addOld($inputArray, $articleId)
    {
        $articleId = intval($articleId);
        
        $sql_data_array = array();
        if(array_key_exists('userId', $inputArray)) $sql_data_array['userId'] = $inputArray['userId'];
        if(!$sql_data_array){
            return "";
        }
                
        try {
            $db = $this->getAdapter();
            $where = $db->quoteInto(" id = ? ", $articleId);
            $rows_affected = $this->update($sql_data_array, $where);
            if($rows_affected){
                $this->_unsetArticleRow($articleId);
            }
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
        
        return "";
    }
    
    private function _unsetArticleRow($articleId)
    {
        if(!$articleId)
        {
            return "";
        }
        
        $article = $this->getRowByArticleId($articleId);
        if(!$article)
        {
            return "";
        }
        unset(self::$_articleRow[$articleId]);
    }
    

}  

